package reader;
import java.util.ArrayList;
import java.util.Iterator;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;


public class QueryPedia {

	private String query="";
		
	public ArrayList<String> runQuery(ArrayList<String> inter){
		ArrayList<String> resultList=new ArrayList<String>();
		String sparqlQueryString = "";
		buildQuery(inter);
		sparqlQueryString= query+" LIMIT 30";
		/*
		sparqlQueryString= "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> "+
						   "PREFIX dbo:<http://dbpedia.org/resource/Category:> "+
						   "SELECT ?article "+
						   "WHERE { "+
						   "{?article skos:subject dbo:Social_media} UNION {?article skos:subject dbo:Semantic_Web} "+
						   "}";
		*/
		/*
		sparqlQueryString= "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> "+
		   "PREFIX dbo:<http://dbpedia.org/resource/Category:> "+
		   "SELECT ?article "+
		   "WHERE { "+
		   "{?article skos:subject dbo:Social_media}"+
		   "}";*/
		
        Query query = QueryFactory.create(sparqlQueryString);

        QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);


        try {
            ResultSet results = qexec.execSelect();
            for ( ; results.hasNext() ; )
        {
            QuerySolution soln = results.nextSolution() ;
            String x = soln.get("article").toString();
            //String a= getPage(x);
            String s[]=x.split("/");
            String temp=s[s.length-1];
            String a="http://en.wikipedia.org/wiki/"+temp;
            resultList.add(a);
            System.out.print(a +"\n");
        }

        }
        finally { qexec.close() ; }
        
        
        return resultList;

	}
	
	public String getPage(String data){
		String result="";
		String sparqlQueryString = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> "+
								   "SELECT ?hasValue "+
								   "WHERE {"+
								   "{ <"+data+"> foaf:page ?hasValue }"+
								   "}";

        Query query = QueryFactory.create(sparqlQueryString);

        QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);


        try {
            ResultSet results = qexec.execSelect();
            QuerySolution soln = results.nextSolution();
            String x = soln.get("hasValue").toString();
            //System.out.print(x +"\n");
            result=x;
        }
        finally { qexec.close() ; }
		
		return result;
	}

	public void buildQuery(ArrayList<String> interests){
		String sparqlQueryString= "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> "+
								  "PREFIX dbo:<http://dbpedia.org/resource/Category:> "+
								  "SELECT ?article "+
								  "WHERE { ";
		int i=1;
	    Iterator<String> it= interests.iterator();
	    while(it.hasNext()){
	    	String interest= it.next();
	    	sparqlQueryString+="{?article skos:subject dbo:"+interest+"} ";
	    	if(i!=interests.size())
	    		sparqlQueryString+=" UNION ";
	    	i++;
	    }
	    sparqlQueryString+="} ORDER BY ?article";
	    
	    query=sparqlQueryString;
	}

}
